package com.letoken.platform.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.constant.ConstantsValue;
import com.letoken.platform.pub.constant.LetokenUserConstant;
import com.letoken.platform.pub.enums.BalanceTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.pub.util.DataCalculationUtils;
import com.letoken.platform.pub.util.LocalDateTool;
import com.letoken.platform.pub.util.ReflexUtils;
import com.letoken.platform.repo.admin.req.mall.AmbListReq;
import com.letoken.platform.repo.admin.req.mall.AmbReleaseListReq;
import com.letoken.platform.repo.admin.req.mall.AmbUpdateReq;
import com.letoken.platform.repo.admin.req.mall.PromotionUpdateReq;
import com.letoken.platform.repo.admin.req.user.AdminBalanceHistoryListReq;
import com.letoken.platform.repo.admin.req.user.AdminBalanceListReq;
import com.letoken.platform.repo.admin.res.mall.AmbBonusReleaseRes;
import com.letoken.platform.repo.admin.res.mall.AmbBonusStatisticsRes;
import com.letoken.platform.repo.admin.res.mall.AmbListRes;
import com.letoken.platform.repo.admin.res.mall.AmbReleaseListRes;
import com.letoken.platform.repo.admin.res.user.AdminBalanceHistoryRes;
import com.letoken.platform.repo.admin.res.user.AdminBalanceHistoryStatisticsRes;
import com.letoken.platform.repo.admin.res.user.AdminBalanceListRes;
import com.letoken.platform.repo.app.dao.AppBalanceHistoryMapper;
import com.letoken.platform.repo.app.dao.AppBalanceWalletMapper;
import com.letoken.platform.repo.app.res.user.AdminBalanceTotalRes;
import com.letoken.platform.repo.mall.dao.*;
import com.letoken.platform.repo.mall.po.AdminMallAmbassador;
import com.letoken.platform.repo.mall.po.AdminMallIssueConfig;
import com.letoken.platform.repo.mall.po.AdminMallPromotion;
import com.letoken.platform.repo.mall.po.AppBalanceWallet;
import com.letoken.platform.repo.utlis.PageDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/balance")
public class AdminBalanceController {

    @Autowired
    AppBalanceWalletMapper appBalanceWalletMapper;
    @Autowired
    AppBalanceHistoryMapper appBalanceHistoryMapper;

    @Permission(uri = "/balance/total", qx = "/balance/total")
    @GetMapping("/total")
    public ApiResponse<AdminBalanceTotalRes> balanceTotal() {
        AppBalanceWallet abw = appBalanceWalletMapper.totalData();
        AppBalanceWallet abw2 = appBalanceWalletMapper.totalIncome();
        AdminBalanceTotalRes res = new AdminBalanceTotalRes();
        if (ObjectUtils.isNotEmpty(abw)) {
            res.setTotalBalance(abw.getBalance());
            res.setTotalIncome(abw2.getIncome());
        }
        return ApiResponse.create(res);
    }

    @Permission(uri ="/balance/list" ,qx = "/balance/list")
    @PostMapping("/list")
    public ApiResponse<PageData<AdminBalanceListRes>> balanceList(@RequestBody @Validated AdminBalanceListReq req) {
        if (ObjectUtils.isNotEmpty(req.getLeId())){
            if (req.getLeId().startsWith("le")){
                req.setLeId(req.getLeId().substring(2));
            }
            try {
                req.setUserId(Integer.valueOf(req.getLeId()));
            } catch (Exception e){
                log.error("转换类型失败",e);
                req.setUserId(null);
            }
        }
        Page<AdminBalanceListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AdminBalanceListRes> res = appBalanceWalletMapper.balanceRecord(page,req);
        return ApiResponse.create(PageDataUtil.getPageData(res));
    }

    @Permission(uri = "/balance/historyStatistics",qx = "/balance/historyStatistics")
    @GetMapping("/historyStatistics")
    public ApiResponse<AdminBalanceHistoryStatisticsRes> historyStatistics() {

        LocalDate yesterday = LocalDate.now().minusDays(1);
        AdminBalanceHistoryStatisticsRes res = new AdminBalanceHistoryStatisticsRes();
        res.setYesterdayIncome(appBalanceHistoryMapper.dayIncome(LocalDateTime.of(yesterday, LocalTime.MIN),LocalDateTime.of(yesterday,LocalTime.MAX), BalanceTypeEnum.INCOME.ordinal()));
        res.setTransferOut(appBalanceHistoryMapper.totalTransfer(BalanceTypeEnum.TRANSFER_OUT.ordinal()));
        res.setTransferIn(appBalanceHistoryMapper.totalTransfer(BalanceTypeEnum.TRANSFER_IN.ordinal()));
        return ApiResponse.create(res);
    }



    @Permission(uri ="/balance/historyList" ,qx = "/balance/historyList")
    @PostMapping("/historyList")
    public ApiResponse<PageData<AdminBalanceHistoryRes>> historyList(@RequestBody @Validated AdminBalanceHistoryListReq req) {
        if (ObjectUtils.isNotEmpty(req.getLeId())){
            if (req.getLeId().startsWith("le")){
                req.setLeId(req.getLeId().substring(2));
            }
            try {
                req.setUserId(Integer.valueOf(req.getLeId()));
            } catch (Exception e){
                log.error("转换类型失败",e);
                req.setUserId(null);
            }
        }
        if (ObjectUtils.isNotEmpty(req.getType())){
            if (req.getType() < 0) req.setType(null);
        }

        Page<AdminBalanceHistoryRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AdminBalanceHistoryRes> res = appBalanceHistoryMapper.adminHistory(page,req);
        if (!CollectionUtils.isEmpty(res.getRecords())) {
            res.getRecords().forEach(x -> {
                x.setTypeDisplay(BalanceTypeEnum.getDesc(x.getType()));
            });
        }
        return ApiResponse.create(PageDataUtil.getPageData(res));
    }


}
